package com.ygs.secs.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ygs.secs.model.Enterprise;
import com.ygs.secs.service.EnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class EnterpriseManagerController {
    @Autowired
    EnterpriseService enterpriseService;

    /**
     * 根据条件查询企业
     *
     * @param enterprise 企业实体json串
     * @return 企业集合
     */
    @RequestMapping(value = "getEnterpriseByAttributes")
    @ResponseBody
    public Map<String, Object> getEnterpriseByAttributes(Enterprise enterprise,
                                                      @RequestParam(defaultValue = "1") int page,
                                                      @RequestParam(defaultValue = "20") int limit) {
        Map<String, Object> resultData = new HashMap<>();
        try {
            Page startPage = PageHelper.startPage(page, limit);
            List<Enterprise> enterprises = enterpriseService.getEnterpriseByAttributes(enterprise);
            //分页查询
            if (enterprises.size()>0){
                resultData.put("code", 200);
                resultData.put("msg", "企业数据查询成功");
                resultData.put("data", enterprises);
                resultData.put("count", startPage.getTotal());
            }else{
                resultData.put("code",201);
                resultData.put("msg","数据为null，请先添加");
            }
            return resultData;
        }catch (Exception e){
            resultData.put("code", 500);
            resultData.put("msg", "system error");
            return resultData;
        }
    }

    /**
     * 删除企业
     *
     * @param enterpriseid
     * @return
     */

    @RequestMapping("/delEnterprise")
    @ResponseBody
    public Map<String, Object> delEnterprise(@RequestParam("enterpriseid") String enterpriseid) throws Exception{
        Map<String, Object> result = new HashMap<>();
        try {
            int res = enterpriseService.deleteByPrimaryKey(enterpriseid);
            if (res > 0) {
                result.put("code", 200);
                result.put("msg", "操作成功");
            } else {
                result.put("code", 500);
                result.put("msg", "操作失败");
            }
            return result;
        }catch (Exception e){
            e.printStackTrace();
            result.put("code", 500);
            result.put("msg", "操作失败，请先删除所属信息");
            return result;
        }
    }

    /**
     * 批量删除企业信息
     *
     * @param enterprises 企业id数组
     * @return 操作结果
     */
    @RequestMapping(value = "/batchDelEnterprise",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> batchDelEnterprise(String[] enterprises)throws Exception {
        Map<String, Object> result = new HashMap<>();
        try {
            int res = enterpriseService.batchDelete(enterprises);
            if (res > 0) {
                result.put("code", 200);
                result.put("msg", "操作成功");
            } else {
                result.put("code", 500);
                result.put("msg", "操作失败");
            }
            return result;
        }catch (Exception e){
            e.printStackTrace();
            result.put("code", 500);
            result.put("msg", "操作失败，请先删除所属信息");
            return result;
        }
    }
}
